/**
 * 2013-6-20 下午9:25:43 Created By niexiaoqiang
 */

package com.xiaoqiang.util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaoqiang.MtwV2ForMoblie.R;

/**
 * mtw对话框
 */
public class MtwDialog extends Dialog {
	private ImageView titleImageView;
	private TextView contentTextview;
	private TextView titleTextview;
	private Button okButton;
	private Button cancelButton;
	private Drawable titleImageMessage;
	private Drawable titleImageError;
	private Drawable titleImageWaring;
	private static Logger log = new Logger(MtwDialog.class);

	private DialogResult result;
	private static Handler mHandler;

	public MtwDialog(Context context) {
		super(context, R.style.MyDialog);
		this.setContentView(R.layout.dialog_layout);
		titleImageView = (ImageView) this.findViewById(R.id.mtw_dialogtitleimage);
		contentTextview = (TextView) this.findViewById(R.id.mtw_dialogcontenttext);
		titleTextview = (TextView) this.findViewById(R.id.mtw_dialogtitletext);
		okButton = (Button) this.findViewById(R.id.mtw_dialogbuttonok);
		okButton.setOnClickListener(new Dialogbuttonlistener());
		cancelButton = (Button) this.findViewById(R.id.mtw_dialogbuttoncancel);
		cancelButton.setOnClickListener(new Dialogbuttonlistener());
		titleImageMessage = context.getResources().getDrawable(R.drawable.mtw_dialogtitleimagemessage);
		titleImageError = context.getResources().getDrawable(R.drawable.mtw_dialogtitleimageerror);
		titleImageWaring = context.getResources().getDrawable(R.drawable.mtw_dialogtitleimagewaring);
		this.setOnCancelListener(new MtwDismissListener(this));
	}

	/**
	 * @param title
	 * @param content
	 */
	public DialogResult showDialog(DialogType type, String content) {
		switch (type) {
			case Waring:
				this.titleTextview.setText(type.toString());
				titleImageView.setBackgroundDrawable(titleImageWaring);
				break;
			case Error:
				this.titleTextview.setText(type.toString());
				titleImageView.setBackgroundDrawable(titleImageError);
				break;
			case Message:
				this.titleTextview.setText(type.toString());
				titleImageView.setBackgroundDrawable(titleImageMessage);
				break;
			default:
				log.error("DialogType is error");
				break;
		}
		this.contentTextview.setText(content);
		mHandler = new DialogHandler();
		this.show();
		try {
			// 启用消息循环
			Looper.getMainLooper();
			Looper.loop();
		}
		catch (RuntimeException e) {
			log.error(e);
		}
		return result;
	}

	public void endDialog(DialogResult result) {
		this.dismiss();
		setDialogResult(result);
		Message m = mHandler.obtainMessage();
		mHandler.sendMessage(m);
	}

	private void setDialogResult(DialogResult result) {
		this.result = result;
	}

	/**
	 * @function 按钮事件监听器
	 * @author Administrator
	 * 
	 */
	class Dialogbuttonlistener implements android.view.View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (((Button) v).getId() == R.id.mtw_dialogbuttonok) {
				endDialog(DialogResult.OK);
			} else {
				endDialog(DialogResult.CANCEL);
			}
		}
	}

	/**
	 * @author niexiaoqiang
	 * @function 防止用户点击屏幕其他地方导致没有返回值的问题
	 */
	class MtwDismissListener implements DialogInterface.OnCancelListener {
		MtwDialog mtwDialog;

		public MtwDismissListener(MtwDialog mtwDialog) {
			this.mtwDialog = mtwDialog;
		}

		@Override
		public void onCancel(DialogInterface dialog) {
			mtwDialog.result = MtwDialog.DialogResult.CANCEL;
			setDialogResult(result);
			Message m = mHandler.obtainMessage();
			mHandler.sendMessage(m);
		}
	}

	static class DialogHandler extends Handler {
		@Override
		public void handleMessage(Message mesg) {
			throw new RuntimeException("message looper throws,don't catch");
		}
	}

	public enum DialogType {
		Waring, Error, Message
	};

	public enum DialogResult {
		OK, CANCEL
	}

}
